package com.pig.graduate.service.impl.finance;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig.graduate.domain.finance.FinanceInfo;
import com.pig.graduate.domain.finance.FinanceStatement;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.financeInfo.FinanceInfoService;
import com.pig.graduate.mapper.finance.FinanceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
* @author 星星眼
* @description 针对表【finance_info】的数据库操作Service实现
* @createDate 2023-04-16 16:25:05
*/
@Service
public class FinanceInfoServiceImpl extends ServiceImpl<FinanceInfoMapper, FinanceInfo>
    implements FinanceInfoService{

    @Autowired
    private FinanceInfoMapper financeInfoMapper;


    @Override
    public MyResult financialStatement(String finaDate) {
        List<FinanceStatement> list = financeInfoMapper.financialStatement(finaDate);
        Map<String,FinanceStatement> map = new HashMap<>();
        for (FinanceStatement financeStatement : list) {
            if(map.containsKey(financeStatement.getFsDate())){
                FinanceStatement finance = map.get(financeStatement.getFsDate());
                if(finance.getTotalPrice().compareTo(BigDecimal.ZERO) == -1){ //小于0 说明是支出
                    finance.setExpenditures(finance.getTotalPrice());
                    finance.setRevenue(financeStatement.getTotalPrice());
                    finance.setTotalProfit(finance.getRevenue().add(finance.getExpenditures()));
                    finance.setProfitMargin(finance.getTotalProfit().divide(finance.getRevenue(),2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)) + "%");
                }else if(finance.getTotalPrice().compareTo(BigDecimal.ZERO) == 1){ //大于0
                    finance.setExpenditures(financeStatement.getTotalPrice());
                    finance.setRevenue(finance.getTotalPrice());
                    finance.setTotalProfit(finance.getRevenue().add(finance.getExpenditures()));
                    finance.setProfitMargin(finance.getTotalProfit().divide(finance.getRevenue(),2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)) + "%");
                }else if(finance.getTotalPrice().compareTo(BigDecimal.ZERO) == 0){ //等于0
                    if(financeStatement.getTotalPrice().compareTo(BigDecimal.ZERO) == 1){
                        finance.setRevenue(financeStatement.getTotalPrice());
                        finance.setTotalProfit(financeStatement.getTotalPrice());
                        finance.setProfitMargin("100%");
                    }else if(financeStatement.getTotalPrice().compareTo(BigDecimal.ZERO) == -1){
                        finance.setExpenditures(financeStatement.getTotalPrice());
                        finance.setTotalProfit(financeStatement.getTotalPrice());
                        finance.setProfitMargin("-100%");
                    }else{
                        finance.setProfitMargin("0%");
                    }
                }
                map.put(finance.getFsDate(),finance);
            }else{
                if(financeStatement.getTotalPrice().compareTo(BigDecimal.ZERO) == 1){
                    financeStatement.setRevenue(financeStatement.getTotalPrice());
                    financeStatement.setTotalProfit(financeStatement.getTotalPrice());
                    financeStatement.setProfitMargin("100%");
                }else if(financeStatement.getTotalPrice().compareTo(BigDecimal.ZERO) == -1){
                    financeStatement.setExpenditures(financeStatement.getTotalPrice());
                    financeStatement.setTotalProfit(financeStatement.getTotalPrice());
                    financeStatement.setProfitMargin("-100%");
                }else{
                    financeStatement.setProfitMargin("0%");
                }
                map.put(financeStatement.getFsDate(),financeStatement);
            }
        }
        List<FinanceStatement> re = new ArrayList(map.values());
        return MyResult.success(re);
    }
}




